package jdbc.test;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class UpdateStudent2 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");//static{}
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        System.out.println("根据学号修改name和wechat号：");
        Scanner in=new Scanner(System.in);
        System.out.println("请输入学号：");
        long stuno=in.nextLong();
        System.out.println("请输入姓名：");
        in.nextLine();//回车不要
        String newName=in.nextLine();
        System.out.println("请输入微信号：");
        String wechat=in.nextLine();

        String url = "jdbc:mysql://localhost:3306/java79school?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        Connection con = null;
        PreparedStatement st = null;
        try {
            String sql = "update s_student set sname=?,wechat=? where stuno=?";
            System.out.println(sql);
            con = DriverManager.getConnection(url, "root", "root1234");
            st = con.prepareStatement(sql);
            st.setString(1,newName);
            st.setString(2,wechat);
            st.setLong(3,stuno);
            int i=st.executeUpdate();//没有sql参数
            System.out.printf("成功了修改%d行数据", i);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null)
                    st.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
